package com.flyco.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.exception.FlycoException;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.mapper.OmsOrderMapper;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.mapper.StmStoreInfoMapper;
import com.flyco.modules.system.entity.SysStoreUser;
import com.flyco.modules.system.entity.SysUser;
import com.flyco.modules.system.mapper.SysAnnouncementSendMapper;
import com.flyco.modules.system.mapper.SysStoreUserMapper;
import com.flyco.modules.system.mapper.SysUserMapper;
import com.flyco.modules.system.service.ISysStoreUserService;
import io.netty.util.internal.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * @Description: 经销商跟单员关系表
 * @Author: flyco
 * @Date: 2024-05-14
 * @Version: V1.0
 */
@Service
public class SysStoreUserServiceImpl extends ServiceImpl<SysStoreUserMapper, SysStoreUser> implements ISysStoreUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private StmStoreInfoMapper stmStoreInfoMapper;
    @Resource
    private OmsOrderMapper omsOrderMapper;
    @Resource
    private SysStoreUserMapper storeUserMapper;

    @Override
    public void saveWithUser(SysStoreUser user) {
        if (StringUtil.isNullOrEmpty(user.getUserName())) {
            throw new FlycoException("用户名不能为空!");
        }
        SysUser sysUser = sysUserMapper.getUserByName(user.getUserName());
        if (sysUser == null) {
            throw new FlycoException("未找到用户!");
        }
        LambdaQueryWrapper<SysStoreUser> storeUserQuery = new LambdaQueryWrapper<>();
        storeUserQuery.eq(SysStoreUser::getStoreId, user.getStoreId())
                .eq(SysStoreUser::getPartnerId, user.getPartnerId())
                .eq(SysStoreUser::getUserName, user.getUserName())
                .eq(SysStoreUser::getChannelId, user.getChannelId());
        List<SysStoreUser> storeUserList = storeUserMapper.selectList(storeUserQuery);
        if (!CollectionUtils.isEmpty(storeUserList)) {
            throw new FlycoException("系统已经存在该经销商跟单员关系，不能重复!");
        }
        //更新经销商跟单员
        LambdaQueryWrapper<StmStoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StmStoreInfo::getCompanyCode, user.getStoreId())
                .eq(StmStoreInfo::getPartnerId, user.getPartnerId())
                .eq(StmStoreInfo::getChannelId, user.getChannelId());
        List<StmStoreInfo> storeInfoList = stmStoreInfoMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(storeInfoList)) {
            storeInfoList.forEach(item -> {
                item.setSaleUserId(sysUser.getId());
                item.setSaleRealName(sysUser.getRealname());
                stmStoreInfoMapper.updateById(item);
            });
        }
        //未传到SAP的订单需要更新跟单员信息
        LambdaQueryWrapper<OmsOrder> orderQuery = new LambdaQueryWrapper<>();
        orderQuery.eq(OmsOrder::getCustomerCode, user.getStoreId())
                .eq(OmsOrder::getPartnerId, user.getPartnerId())
                .eq(OmsOrder::getChannelId, user.getChannelId())
                .eq(OmsOrder::getStatus, 0);
        List<OmsOrder> orderList = omsOrderMapper.selectList(orderQuery);
        if (orderList != null && !orderList.isEmpty()) {
            orderList.forEach(item -> {
                item.setSaleUserId(Long.valueOf(sysUser.getId()));
                item.setSaleUserName(sysUser.getRealname());
                omsOrderMapper.updateById(item);
            });
        }
        if (!StringUtil.isNullOrEmpty(user.getId())) {
            updateById(user);
        } else {
            save(user);
        }
    }

    @Override
    public void importData() {
        List<SysStoreUser> storeUserList = list();
        if (!CollectionUtils.isEmpty(storeUserList)) {
            storeUserList.forEach(x -> {
                SysUser sysUser = sysUserMapper.getUserByName(x.getUserName());
                if (sysUser != null) {
                    LambdaQueryWrapper<StmStoreInfo> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(StmStoreInfo::getCompanyCode, x.getStoreId())
                            .eq(StmStoreInfo::getPartnerId, x.getPartnerId())
                            .eq(StmStoreInfo::getChannelId, x.getChannelId());
                    List<StmStoreInfo> storeInfoList = stmStoreInfoMapper.selectList(queryWrapper);
                    if (!CollectionUtils.isEmpty(storeInfoList)) {
                        storeInfoList.forEach(item -> {
                            item.setSaleUserId(sysUser.getId());
                            item.setSaleRealName(sysUser.getRealname());
                            stmStoreInfoMapper.updateById(item);
                        });
                    }
                    //未传到SAP的订单需要更新跟单员信息
                    LambdaQueryWrapper<OmsOrder> orderQuery = new LambdaQueryWrapper<>();
                    orderQuery.eq(OmsOrder::getCustomerCode, x.getStoreId())
                            .eq(OmsOrder::getPartnerId, x.getPartnerId())
                            .eq(OmsOrder::getChannelId, x.getChannelId())
                            .eq(OmsOrder::getStatus, 0);
                    List<OmsOrder> orderList = omsOrderMapper.selectList(orderQuery);
                    {
                        orderList.forEach(item -> {
                            item.setSaleUserId(Long.valueOf(sysUser.getId()));
                            item.setSaleUserName(sysUser.getRealname());
                        });
                    }
                }
            });
        }
    }

    @Override
    public void updateStoreUser(SysStoreUser storeUser) {
        StmStoreInfo storeInfo = stmStoreInfoMapper.selectById(storeUser.getStoreId());
        if (storeInfo == null) {
            throw new FlycoException("未找到该经销商!");
        }
        SysUser user = sysUserMapper.selectById(storeUser.getUserName());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        storeInfo.setSaleUserId(storeUser.getUserName());
        storeInfo.setSaleRealName(storeUser.getRealName());
        storeInfo.setUpdateTime(new Date());
        storeInfo.setUpdateBy(sysUser.getRealname());
        //更新经销商上的跟单员信息
        stmStoreInfoMapper.updateById(storeInfo);

        LambdaQueryWrapper<SysStoreUser> storeUserQuery = new LambdaQueryWrapper<>();
        storeUserQuery.eq(SysStoreUser::getStoreId, storeInfo.getCompanyCode())
                .eq(SysStoreUser::getPartnerId, storeInfo.getPartnerId())
                .eq(SysStoreUser::getChannelId, storeInfo.getChannelId());

        List<SysStoreUser> storeUserList = storeUserMapper.selectList(storeUserQuery);
        if (storeUserList.isEmpty()) {
            SysStoreUser sysStoreUser = new SysStoreUser();
            sysStoreUser.setPartnerId(Integer.valueOf(storeInfo.getPartnerId().toString()));
            sysStoreUser.setStoreId(storeInfo.getCompanyCode());
            sysStoreUser.setChannelId(storeInfo.getChannelId());
            sysStoreUser.setUserName(user.getUsername());
            sysStoreUser.setCreateBy(sysUser.getRealname());
            sysStoreUser.setCreateTime(new Date());
            storeUserMapper.insert(sysStoreUser);
        } else {
            SysStoreUser sysStoreUser = storeUserList.get(0);
            sysStoreUser.setUserName(user.getUsername());
            sysStoreUser.setUpdateBy(sysUser.getRealname());
            sysStoreUser.setUpdateTime(new Date());
            storeUserMapper.updateById(sysStoreUser);
        }
    }
}
